﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WindowsFormsApp1.IICMoudle
{
    public static class IOoperation
    {
        //设置IO输出方式：IoCfg:0=输入; IoCfg:2=OD+上拉, 3=PushPull推挽输出;
        [DllImport("lib/USB2IO.dll", EntryPoint = "USB2IO_SetIoCfg", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int USB2IO_SetIoCfg(IntPtr USB2IO_hdl, int IoNbr, int IoCfg);

        //读取IO引脚值(这里读取到的是实际引脚的电平值), 读取值存放在PinValue中
        [DllImport("lib/USB2IO.dll", EntryPoint = "USB2IO_RdPin", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int USB2IO_RdPin(IntPtr USB2IO_hdl, int IoNbr, ref int PinValue);

        //打开端口获取句柄;  //Nbr是端口号，从1开始，依次为2/3/4...，最大126
        [DllImport("lib/USB2IO.dll", EntryPoint = "USB2IO_Open", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr USB2IO_Open(int Nbr);

        //设置IO输出值：  IoOut:1=输出高, 0=输出低
        [DllImport("lib/USB2IO.dll", EntryPoint = "USB2IO_SetIoOut", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int USB2IO_SetIoOut(IntPtr USB2IO_hdl, int IoNbr, int IoOut);

        //关闭端口；在程序退出前再关闭端口; 返回 0: 成功；!0: 失败
        [DllImport("lib/USB2IO.dll", EntryPoint = "USB2IO_Close", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int USB2IO_Close(IntPtr USB2IO_hdl);

        private static IntPtr USB2IO_Handle;

        public static int PinValue_I03 = 0;
        public static int PinValue_I05 = 0;

        public static bool IOInit()
        {
            USB2IO_Handle = USB2IO_Open(1);
            if ((int)USB2IO_Handle == -1)
            {
                return false;
            }
            USB2IO_SetIoCfg(USB2IO_Handle, 10, 3);
            USB2IO_SetIoOut(USB2IO_Handle, 10, 0);
            return true;
        }

        public static void ResetModule()
        {
            //USB2IO_SetIoCfg(USB2IO_Handle, 8, 3);
            //Console.WriteLine(1);
            //USB2IO_SetIoOut(USB2IO_Handle, 8, 1);
            //Console.WriteLine(2);
            //MyDelay.Delay(10);
            //USB2IO_SetIoOut(USB2IO_Handle, 8, 0);
            //Console.WriteLine(3);
            //MyDelay.Delay(10);
            //USB2IO_SetIoOut(USB2IO_Handle, 8, 1);
            //Console.WriteLine(4);
            //MyDelay.Delay(10);
            //USB2IO_SetIoCfg(USB2IO_Handle, 8, 0);
            //Console.WriteLine(5);
            VBRTOFF();
            Thread.Sleep(1000);
            VBRTON();
        }

        public static void VBRTON()
        {
            USB2IO_SetIoCfg(USB2IO_Handle, 10, 3);
            USB2IO_SetIoOut(USB2IO_Handle, 10, 0);
            Thread.Sleep(300);
        }

        public static void VBRTOFF()
        {
            USB2IO_SetIoCfg(USB2IO_Handle, 10, 3);
            USB2IO_SetIoOut(USB2IO_Handle, 10, 1);
            Thread.Sleep(200);
        }

        public static void ReadIoCfg()
        {
            USB2IO_SetIoCfg(USB2IO_Handle, 3, 2);
            USB2IO_SetIoOut(USB2IO_Handle, 3, 1);
            USB2IO_SetIoCfg(USB2IO_Handle, 5, 2);
            USB2IO_SetIoOut(USB2IO_Handle, 5, 1);
            USB2IO_RdPin(USB2IO_Handle, 3, ref PinValue_I03);
            USB2IO_RdPin(USB2IO_Handle, 5, ref PinValue_I05);
        }

        public static void IOClose()
        {
            if ((int)USB2IO_Handle > 0)
            {
                USB2IO_Close(USB2IO_Handle);
                PinValue_I03 = 0;
                PinValue_I05 = 0;
            }
        }

        public static void VBRTOFF(int time)
        {
            if ((int)USB2IO_Handle == -1)
            {
                return;
            }
            USB2IO_SetIoOut(USB2IO_Handle, 10, 1);
            Sleep(time);
            USB2IO_SetIoOut(USB2IO_Handle, 10, 0);
        }

        public static void Sleep(int time)
        {
            if (time <= 0)
            {
                return;
            }
            DateTime start = DateTime.Now;
            while (DateTime.Now.Subtract(start).TotalMilliseconds < time)
            {
            }
        }
    }
}
